<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>One Salt Backend</title>
  <script src="./js/onesalt.js"></script>
</head>
<body>
  <div class="panel-heading">
    <div class="container">
      <div class="row">
        <div class="col-md-10">
          <h1>『一味盐』后台操作管理系统</h1>
        </div>
        <form action="/logout" method="POST">
          <input class="btn btn-link pull-right" type="submit" value="Logout" />
        </form>
      </div>
    </div>
  </div>
  <nav class="navbar navbar-default">
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#menu-top">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="#">一味盐</a>
      </div>
      <div class="collapse navbar-collapse" id="menu-top">
        <ul class="nav navbar-nav">
        </ul>
      </div>
      <script>
        var menus;
        
        $(document).ready(function(e) {
          reloadMenus();
        });
        
        function reloadMenus() {
          $.ajax({
            url: 'data/menu.json',
            dataType: 'json',
            success: function(data) {
              menus = data;
              loadTopMenus();
            },
            error: function() {
              alert('load menus error');
            }
          });
        }
        
        function loadTopMenus() {
          var ul = $("#menu-top ul");
          ul.empty();
          _.each(menus, function(menu, index) {
            var li = $("<li><a href='#'>" + menu.title + "</a></li>");
            ul.append(li);
            li.click(function(e) {
              if($(this).hasClass("active")) {
                return;
              }
              var actived = $(this).siblings(".active");
              if(_.isObject(actived) && actived.length > 0) {
                actived.removeClass("active");
              }
              $(this).addClass("active");
              $('#content').empty();
              loadLeftMenus(menu);
            });
            
            if(index == 0) {
              li.click(); 
            }
          });
        }

        function loadLeftMenus(menu) {
            var list = menu.children;
            var root = $('#menu-left');
            
            root.empty();
            var panelHeader = _.template('<div class="panel-heading"><strong><%=title%></strong></div>');
            _.each(list, function(i) {
              if(_.isEmpty(i.title) && _.isEmpty(i.children)) {
                return;
              }
              var panel = $('<div class="panel panel-default"></div>');
              var sublist = $('<div class="list-group"></div>');
              if(!_.isEmpty(i.title) && !_.isEmpty(i.children)) {
                panel.append($(panelHeader({title: i.title})));
                _.each(i.children, function(j) {
                  createLeftMenuItem(sublist, j);
                });
              } else {
                createLeftMenuItem(sublist, i);
              }
              panel.append(sublist);
              root.append(panel);
            });
        }
        
        function createLeftMenuItem(listGroup, menu) {
          var compiledItem = _.template('<a class="list-group-item" href="#"><%=title%></a>');
          var listItem = $(compiledItem({title: menu.title}));
          listItem.click(function(e) {
            $('#content').empty();
            var url = menu.url;
            loadContent(url);
          });
          listGroup.append(listItem);
        }
      </script>
    </div>
  </nav>
  <div class="container">
    <div class="row">
      <div class="col-md-2" id="menu-left">
      </div>
      <div class="col-md-10" id="content">
        <!-- show content -->
      </div>
    </div>
  </div>
</body>
</html>